package com.wpen.unit.infra.repository.job.impl;

import java.io.Serializable;
import java.util.List;

import com.wpen.unit.infra.repository.job.TaskExecRedologRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.wpen.unit.exception.UpdateZeroException;
import com.wpen.unit.infra.mybatis.job.mapper.TaskExecRedologMapper;
import com.wpen.unit.infra.mybatis.job.po.TaskExecRedologPo;
import com.wpen.unit.infra.mybatis.job.po.TaskExecRedologPo.TaskExecRedologColumns;
import com.wpen.unit.utils.StringUtil;

/**
 * 任务执行重跑历史日志维护.
 * 
 * @author Wang Peng
 * @date 2023-01-18
 *
 */
@Repository
@Transactional
public class TaskExecRedologRepositoryImpl extends ServiceImpl<TaskExecRedologMapper, TaskExecRedologPo> implements TaskExecRedologRepository {
    
    @Override
    public long count(TaskExecRedologPo taskExecRedologPo){
		QueryWrapper<TaskExecRedologPo> queryWrapper = createQuery(taskExecRedologPo);
		return super.count(queryWrapper);
    }

	@Override
    public List<TaskExecRedologPo> queryList(int index, int size, TaskExecRedologPo taskExecRedologPo){
        QueryWrapper<TaskExecRedologPo> queryWrapper = createQuery(taskExecRedologPo);
		if (size > 0) {
			Page<TaskExecRedologPo> page = new Page<>(index, size, false);
			return super.page(page, queryWrapper).getRecords();
		}
		//queryWrapper.orderBy(true, false, TaskExecRedologColumns.ID.name());
		return getBaseMapper().selectList(queryWrapper);
    }
    
    private QueryWrapper<TaskExecRedologPo> createQuery(TaskExecRedologPo taskExecRedologPo) {
        QueryWrapper<TaskExecRedologPo> queryWrapper = Wrappers.query();
        if (taskExecRedologPo.getId() != null) {
            queryWrapper.eq(TaskExecRedologColumns.ID.name(), taskExecRedologPo.getId());
        }
        if (taskExecRedologPo.getTaskLogId() != null) {
            queryWrapper.eq(TaskExecRedologColumns.TASK_LOG_ID.name(), taskExecRedologPo.getTaskLogId());
        }
        if (taskExecRedologPo.getBatchExecLogId() != null) {
            queryWrapper.eq(TaskExecRedologColumns.BATCH_EXEC_LOG_ID.name(), taskExecRedologPo.getBatchExecLogId());
        }
        if (StringUtil.isNotEmpty(taskExecRedologPo.getBizCode())) {
            queryWrapper.eq(TaskExecRedologColumns.BIZ_CODE.name(), taskExecRedologPo.getBizCode());
        }
        if (StringUtil.isNotEmpty(taskExecRedologPo.getBatchCode())) {
            queryWrapper.eq(TaskExecRedologColumns.BATCH_CODE.name(), taskExecRedologPo.getBatchCode());
        }
        if (StringUtil.isNotEmpty(taskExecRedologPo.getTaskCode())) {
            queryWrapper.eq(TaskExecRedologColumns.TASK_CODE.name(), taskExecRedologPo.getTaskCode());
        }
        return queryWrapper;
    }
    
    @Override
    public TaskExecRedologPo queryFirst(TaskExecRedologPo taskExecRedologPo) {
        QueryWrapper<TaskExecRedologPo> queryWrapper = Wrappers.query();
        if (taskExecRedologPo.getId() != null) {
            queryWrapper.eq(TaskExecRedologColumns.ID.name(), taskExecRedologPo.getId());
        }
        if (taskExecRedologPo.getTaskLogId() != null) {
            queryWrapper.eq(TaskExecRedologColumns.TASK_LOG_ID.name(), taskExecRedologPo.getTaskLogId());
        }
        if (taskExecRedologPo.getBatchExecLogId() != null) {
            queryWrapper.eq(TaskExecRedologColumns.BATCH_EXEC_LOG_ID.name(), taskExecRedologPo.getBatchExecLogId());
        }
        if (StringUtil.isNotEmpty(taskExecRedologPo.getBizCode())) {
            queryWrapper.eq(TaskExecRedologColumns.BIZ_CODE.name(), taskExecRedologPo.getBizCode());
        }
        if (StringUtil.isNotEmpty(taskExecRedologPo.getBatchCode())) {
            queryWrapper.eq(TaskExecRedologColumns.BATCH_CODE.name(), taskExecRedologPo.getBatchCode());
        }
        if (StringUtil.isNotEmpty(taskExecRedologPo.getTaskCode())) {
            queryWrapper.eq(TaskExecRedologColumns.TASK_CODE.name(), taskExecRedologPo.getTaskCode());
        }
		//queryWrapper.orderBy(true, false, TaskExecRedologPoColumns.ID.name());
		return super.getOne(queryWrapper);
    }
    
    @Override
    public TaskExecRedologPo queryById(Serializable id) {
        return super.getById(id);
    }
    
    @Override
    public boolean insert(TaskExecRedologPo taskExecRedologPo) {
//        taskExecRedologPo.setVersion(1);
        taskExecRedologPo.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
        return super.save(taskExecRedologPo);
    }

    @Override
    public boolean insertBatch(List<TaskExecRedologPo> listTaskExecRedologPos) {
        int rs = getBaseMapper().insertBatchSomeColumn(listTaskExecRedologPos);
        return SqlHelper.retBool(rs);
    }

    @Override
    public boolean updateById(TaskExecRedologPo taskExecRedologPo) {
        UpdateWrapper<TaskExecRedologPo> updateWrapper = Wrappers.update();
        updateWrapper.eq(TaskExecRedologColumns.ID.name(), taskExecRedologPo.getId());
//        updateWrapper.eq(TaskExecRedologColumns.VERSION.name(), taskExecRedologPo.getVersion());
//        taskExecRedologPo.setVersion(taskExecRedologPo.getVersion() + 1);
        boolean rs = super.update(taskExecRedologPo, updateWrapper);
        // 根据ID更新所有字段，空值也更新
        // int num = getBaseMapper().alwaysUpdateSomeColumnById(ucpBatchInfoPo);
        // boolean rs = SqlHelper.retBool(num);
        if (!rs) {
            throw new UpdateZeroException();
        }
        return rs;
    }

    @Override
    public boolean deleteByIds(Serializable[] ids) {
        UpdateWrapper<TaskExecRedologPo> updateWrapper = Wrappers.update();
		updateWrapper.in(TaskExecRedologColumns.ID.name(), (Object[]) ids);
		return super.remove(updateWrapper);
    }
    
//    public TaskExecRedologPo queryByExample(TaskExecRedologPo taskExecRedologPo) {
//        QueryWrapper<TaskExecRedologPo> queryWrapper = Wrappers.query();
//        if (taskExecRedologPo.getId() != null) {
//            queryWrapper.eq(TaskExecRedologColumns.ID.name(), taskExecRedologPo.getId());
//        }
//        if (taskExecRedologPo.getTaskLogId() != null) {
//            queryWrapper.eq(TaskExecRedologColumns.TASK_LOG_ID.name(), taskExecRedologPo.getTaskLogId());
//        }
//        if (taskExecRedologPo.getBatchExecLogId() != null) {
//            queryWrapper.eq(TaskExecRedologColumns.BATCH_EXEC_LOG_ID.name(), taskExecRedologPo.getBatchExecLogId());
//        }
//        if (StringUtil.isNotEmpty(taskExecRedologPo.getBizCode())) {
//            queryWrapper.eq(TaskExecRedologColumns.BIZ_CODE.name(), taskExecRedologPo.getBizCode());
//        }
//        if (StringUtil.isNotEmpty(taskExecRedologPo.getBatchCode())) {
//            queryWrapper.eq(TaskExecRedologColumns.BATCH_CODE.name(), taskExecRedologPo.getBatchCode());
//        }
//        if (StringUtil.isNotEmpty(taskExecRedologPo.getTaskCode())) {
//            queryWrapper.eq(TaskExecRedologColumns.TASK_CODE.name(), taskExecRedologPo.getTaskCode());
//        }
//        if (taskExecRedologPo.getBatchTriggerTime() != null) {
//            queryWrapper.eq(TaskExecRedologColumns.BATCH_TRIGGER_TIME.name(), taskExecRedologPo.getBatchTriggerTime());
//        }
//        if (taskExecRedologPo.getTaskTriggerTime() != null) {
//            queryWrapper.eq(TaskExecRedologColumns.TASK_TRIGGER_TIME.name(), taskExecRedologPo.getTaskTriggerTime());
//        }
//        if (StringUtil.isNotEmpty(taskExecRedologPo.getAzCode())) {
//            queryWrapper.eq(TaskExecRedologColumns.AZ_CODE.name(), taskExecRedologPo.getAzCode());
//        }
//        if (StringUtil.isNotEmpty(taskExecRedologPo.getDbCode())) {
//            queryWrapper.eq(TaskExecRedologColumns.DB_CODE.name(), taskExecRedologPo.getDbCode());
//        }
//        if (StringUtil.isNotEmpty(taskExecRedologPo.getScheduleType())) {
//            queryWrapper.eq(TaskExecRedologColumns.SCHEDULE_TYPE.name(), taskExecRedologPo.getScheduleType());
//        }
//        if (StringUtil.isNotEmpty(taskExecRedologPo.getExecDate())) {
//            queryWrapper.eq(TaskExecRedologColumns.EXEC_DATE.name(), taskExecRedologPo.getExecDate());
//        }
//        if (StringUtil.isNotEmpty(taskExecRedologPo.getExecStatus())) {
//            queryWrapper.eq(TaskExecRedologColumns.EXEC_STATUS.name(), taskExecRedologPo.getExecStatus());
//        }
//        if (StringUtil.isNotEmpty(taskExecRedologPo.getExecResult())) {
//            queryWrapper.eq(TaskExecRedologColumns.EXEC_RESULT.name(), taskExecRedologPo.getExecResult());
//        }
//        if (StringUtil.isNotEmpty(taskExecRedologPo.getStartupTime())) {
//            queryWrapper.eq(TaskExecRedologColumns.STARTUP_TIME.name(), taskExecRedologPo.getStartupTime());
//        }
//        if (StringUtil.isNotEmpty(taskExecRedologPo.getEndTime())) {
//            queryWrapper.eq(TaskExecRedologColumns.END_TIME.name(), taskExecRedologPo.getEndTime());
//        }
//        if (StringUtil.isNotEmpty(taskExecRedologPo.getCreateTime())) {
//            queryWrapper.eq(TaskExecRedologColumns.CREATE_TIME.name(), taskExecRedologPo.getCreateTime());
//        }
//		Page<TaskExecRedologPo> page = new Page<>(0, 1, false);
//		queryWrapper.orderBy(true, false, TaskExecRedologPoColumns.ID.name());
//    }

}
